package com.yanqu.road.server.manager.activity;

import com.yanqu.road.dao.impl.cross.CrossActivityTeamRankDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.rank.cross.CrossActivityTeamRank;
import com.yanqu.road.entity.rank.cross.TeamBaseInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityTeamRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityTeamRankMgr extends TempMgr {


    /**
     * 活动队伍排名 活动，条件类型，榜单
     */
//    private static Map<Integer, Map<Integer, CrossActivityTeamRankListModel>> crossTeamRankModelMap;
    private static Map<Integer, Map<Integer, ActivityTeamRankModelGroup>> crossTeamRankGroupMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        initCrossActivityTeamRankListModelMap();
        return true;
    }

    private void initCrossActivityTeamRankListModelMap() {
        Map<Integer, Map<Integer, ActivityTeamRankModelGroup>> tempTeamRankGroupMap = new ConcurrentHashMap<>();

        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.DouLuoDaLuActivity.getValue());
        activityTypeList.add(eActivityType.CrossDunHuangShiKuActivity.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap) {
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                        if (activityGroup.getActivityId() == activityInfo.getActivityId()) {
                            List<CrossActivityTeamRank> tempRankList = new CrossActivityTeamRankDaoImpl().getCrossActivityTeamRankList(activityGroup.getActivityId(), activityGroup.getGroupId());

                            Map<Integer, ActivityTeamRankModelGroup> groupMap = tempTeamRankGroupMap.get(activityGroup.getActivityId());
                            if (groupMap == null) {
                                groupMap = new ConcurrentHashMap<>();
                                tempTeamRankGroupMap.put(activityGroup.getActivityId(), groupMap);
                            }
                            ActivityTeamRankModelGroup modelGroup = groupMap.get(activityGroup.getGroupId());
                            if (modelGroup == null) {
                                modelGroup = new ActivityTeamRankModelGroup(activityGroup.getActivityId(), activityGroup.getGroupId());
                                groupMap.put(activityGroup.getGroupId(), modelGroup);
                            }
                            modelGroup.initRankList(tempRankList);
                        }
                    }
                }
            }
        }
        crossTeamRankGroupMap = tempTeamRankGroupMap;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for (Map<Integer, ActivityTeamRankModelGroup> groupMap : crossTeamRankGroupMap.values()) {
            for (ActivityTeamRankModelGroup modelGroup : groupMap.values()) {
                modelGroup.save();
            }
        }
        return true;
    }


//    public static Map<Integer, CrossActivityTeamRankListModel> getCrossActivityTeamRankListModelMap(int activityId){
//        return crossTeamRankModelMap.get(activityId);
//    }

    public static CrossActivityTeamRankListModel getCrossActivityTeamRankListModel(int activityId, int conditionType, int groupId) {
        Map<Integer, ActivityTeamRankModelGroup> groupMap = crossTeamRankGroupMap.get(activityId);
        if (groupMap == null) {
            return null;
        }
        ActivityTeamRankModelGroup modelGroup = groupMap.get(groupId);
        if (modelGroup == null) {
            return null;
        }
        return modelGroup.getRankModelOrNull(conditionType);
    }

    public static CrossActivityTeamRankListModel getCrossActivityTeamRankListModelNewIfNull(int activityId, int conditionType, int groupId) {
        Map<Integer, ActivityTeamRankModelGroup> groupMap = crossTeamRankGroupMap.get(activityId);
        if (groupMap == null) {
            synchronized (crossTeamRankGroupMap) {
                groupMap = crossTeamRankGroupMap.get(activityId);
                if (groupMap == null) {
                    groupMap = new ConcurrentHashMap<>();
                    crossTeamRankGroupMap.put(activityId, groupMap);
                }
            }
        }
        ActivityTeamRankModelGroup modelGroup = groupMap.get(groupId);
        if (modelGroup == null) {
            synchronized (crossTeamRankGroupMap) {
                modelGroup = groupMap.get(groupId);
                if (modelGroup == null) {
                    modelGroup = new ActivityTeamRankModelGroup(activityId, groupId);
                    groupMap.put(groupId, modelGroup);
                }
            }
        }
        return modelGroup.getRankModelNewIfNull(conditionType);
    }

    public static int getCrossActivityTeamRank(int activityId, int conditionType, long serverId, int teamId, int groupId) {
        int rank = -1;
        CrossActivityTeamRankListModel rankListModel = getCrossActivityTeamRankListModel(activityId, conditionType, groupId);
        if (null != rankListModel) {
            rank = rankListModel.getMyRank(teamId, serverId);
        }
        return rank;
    }

    /**
     * 跨服活动排行数据变化（队伍）
     */
    public static void changeActivityTeamRank(int activityId, int conditionType, int teamId, long serverId, BigInteger value, TeamBaseInfo teamBaseInfo, String param, int groupId) {
        CrossActivityTeamRank teamRank = new CrossActivityTeamRank();
        teamRank.setGroupId(groupId);
        teamRank.setType(activityId);
        teamRank.setBigRankType(conditionType);
        teamRank.setTeamId(teamId);
        teamRank.setServerId(serverId);
        teamRank.setValue(value);
        teamRank.setTeamBaseInfo(teamBaseInfo);
        if (!StringUtils.isNullOrEmpty(param)) {
            teamRank.setParam(param);
        }
        changeActivityTeamRank(activityId, conditionType, teamRank);
    }

    /**
     * 跨服活动排行数据变化（队伍）
     */
    public static void changeActivityTeamRank(int activityId, int conditionType, CrossActivityTeamRank teamRank){
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            getLogger().error("changeActivityTeamRank activityInfo is null, activityId:{}", activityId);
            return;
        }
        CrossActivityTeamRankListModel rankListModel = getCrossActivityTeamRankListModelNewIfNull(activityId, conditionType, teamRank.getGroupId());
        if(null != rankListModel){
            rankListModel.rankChange(teamRank);
        }
    }
}
